package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private DishMapper dishMapper;


    /**
     * 新增套餐
     *
     * @param setmealDTO 新增套餐时传递的数据模型
     */
    @Transactional
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 向套餐表插入数据
        setMealMapper.insert(setmeal);
        // 获取生成的套餐id
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
        // 保存套餐和菜品的关联关系
        setMealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO 套餐分页查询时传递的数据模型
     * @return 查询结果
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setMealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除套餐
     *
     * @param ids 套餐id
     */
    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
/*
        // 判断套餐是否起售
        for (Long id : ids) {
            Setmeal setmeal = setMealMapper.getById(id);
            if (Objects.equals(setmeal.getStatus(), StatusConstant.ENABLE)) {
                // 套餐起售中，不能执行删除操作
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        for (Long id : ids) {
            // 删除套餐与菜品的关联关系
            setMealDishMapper.deleteBySetmealId(id);
            // 删除套餐
            setMealMapper.deleteById(id);
        }
*/
        // 判断套餐是否起售
        List<Setmeal> setMeals = setMealMapper.getByIds(ids);
        for (Setmeal setmeal : setMeals) {
            if (Objects.equals(setmeal.getStatus(), StatusConstant.ENABLE)) {
                // 套餐起售中，不能被删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        // 删除套餐与菜品的关联关系
        setMealDishMapper.deleteBySetmealIds(ids);
        // 删除套餐
        setMealMapper.deleteByIds(ids);
    }

    /**
     * 根据id查询套餐及其关联的菜品
     *
     * @param id id
     * @return 查询结果
     */
    @Override
    public SetmealVO getByIdWithDish(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        // 根据id查询套餐数据
        Setmeal setmeal = setMealMapper.getById(id);
        BeanUtils.copyProperties(setmeal, setmealVO);
        // 查询关联的菜品
        List<SetmealDish> setmealDishes = setMealDishMapper.getBySetMealId(setmealVO.getId());
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO 修改套餐时传递的数据模型
     */
    @Transactional
    @Override
    public void updateWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 修改套餐表的基础信息
        setMealMapper.update(setmeal);
        // 删除原有的对应的套餐菜品关联数据
        setMealDishMapper.deleteBySetmealId(setmeal.getId());
        // 插入菜品数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (!setmealDishes.isEmpty()) {
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDTO.getId()));
            setMealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 套餐起售停售
     *
     * @param status 状态
     * @param id     套餐id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        // 如果为套餐停售转起售，则需要判断套餐内菜品是否为起售
        if (Objects.equals(status, StatusConstant.ENABLE)) {
            // 获取套餐菜品关联集合
            List<SetmealDish> setmealDishes = setMealDishMapper.getBySetMealId(id);
            List<Long> ids = new ArrayList<>();
            setmealDishes.forEach(setmealDish -> ids.add(setmealDish.getDishId()));
            // 获取关联的菜品集合
            List<Dish> dishes = dishMapper.getByIds(ids);
            dishes.forEach(dish -> {
                if (Objects.equals(dish.getStatus(), StatusConstant.DISABLE)) {
                    // 存在未起售菜品，无法修改套餐状态
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setMealMapper.update(setmeal);
    }


    /**
     * 条件查询
     *
     * @param setmeal 套餐
     * @return 查询结果
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        return setMealMapper.list(setmeal);
    }

    /**
     * 根据id查询菜品列表
     *
     * @param id 套餐id
     * @return 菜品列表
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setMealDishMapper.getDishItemBySetmealId(id);
    }


}
